// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

        bgSource: {
            default: [],
            type: [cc.SpriteFrame]
        },

        bgSprite: {
            default: [],
            type: [cc.Node]
        },

        moveSpeed: 0,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.level = 0;
    },

    initInfo(level){
        if(this.bgSource != null){
            if(level > this.bgSource.length-1) level = this.bgSource.length-1;
            this.level = level;

            for (var i = this.bgSprite.length - 1; i >= 0; i--) {
                if (this.bgSprite[i]!=null) {
                    this.bgSprite[i].getComponent(cc.Sprite).spriteFrame = this.bgSource[this.level];
                }
                
            }
        }
    },

    start () {

    },

    update (dt) {
        this.node.y -= this.moveSpeed * dt;

        var moveDistance = -this.node.y;

        var muit = Math.round(moveDistance / this.bgSprite[0].height - 0.5);

        for (var i = 0; i < this.bgSprite.length; i++) {
            if (this.bgSprite[i] != null) {
                var tempMuit = i;
                if(muit - i>0) tempMuit += Math.ceil((muit - i) / 3) * 3;

                this.bgSprite[i].y = this.bgSprite[i].height * tempMuit;
            }
        }

    },
});
